package cn.com.cfae.iras.doc.analyze.parse.extractor;

import cn.com.cfae.iras.doc.analyze.keys.ExtractMethod;
import cn.com.cfae.iras.doc.analyze.parse.kv.KVText;
import cn.com.cfae.iras.doc.analyze.parse.model.*;
import cn.com.cfae.iras.doc.analyze.parse.tokenizer.SYTokenizer;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

public class M1Extractor extends MTableExtractor {

    private static final Logger logger = LoggerFactory.getLogger(M1Extractor.class);

    private SYTokenizer syTokenizer = SYTokenizer.getInstance();

    @Override
    public void extract() {
        try {
            extractM1();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

    private void extractM1() throws Exception {
        long t1 = System.currentTimeMillis();
        logger.info("开始抽取：{}章节业务模型数据，表格类型：{}......", getChapterModel().getChapterId(), getDocumentModel().getTableType());

        try {
            extractM1Sy();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        long t2 = System.currentTimeMillis();
        logger.info("抽取：{}章节业务模型数据完成，表格类型：{}，耗时：{}毫秒。", getChapterModel().getChapterId(), getDocumentModel().getTableType(), (t2 - t1));
    }

    /**
     * M-1名词释义
     *
     * @param
     */
    private void extractM1Sy() {
        long t1 = System.currentTimeMillis();
        logger.info("开始抽取{}章节名词释义业务模型，默认抽取第二页......", getChapterModel().getChapterId());
        List<PageModel> pageModelList = getPageModelList();
        ExtractBizModel extractBizModel = getChapterModel().getModel(SYModel.MODEL_NAME);
        ExtractMethod[] extractMethods = extractBizModel.getExtractMethods();
        PageModel pageModel = null;
        syTokenizer.loadCoreSynonyms(extractBizModel);
        for (int i = 0; i < pageModelList.size(); i++) {
            pageModel = pageModelList.get(i);
            if (pageModel != null) {
                List<String> extractTableDataList = null;
                ExtractMethod extractMethod = extractMethods[0];
                switch (extractMethod) {
                    case TEXT:
                        break;
                    case KV:
                        try {
                            extractTableDataList = extractPdfByTable(new String[]{getDocumentModel().getPdfFilePath(), "-l", "-p", String.valueOf(pageModel.getPageNumber()), "-a", pageModel.getDefaultPageArea().getLayout(), "-f", "JSON"});
                        } catch (Exception e) {
                            logger.error(e.getMessage(), e);
                        }
                        if (CollectionUtils.isEmpty(extractTableDataList)) {
                            logger.info("抽取{}章名词释义业务模型，数据格式为文本形式，适用默认文本值进行分析，分析文本：{}。", getChapterModel().getChapterId(), pageModel.getJsonTextData());
                            JsonArray dataArray = getPageDataArray(pageModel);
                            ExtractItem extractItem = null;
                            for (int j = 0; j < dataArray.size(); j++) {
                                JsonArray lineArray = dataArray.get(j).getAsJsonArray();
                                String combineLine = combineTextJsonArray(lineArray);
                                if (isEmpty(combineLine)) {
                                    continue;
                                }
                                if (isStatement(combineLine)) {
                                    continue;
                                }
                                //20181220修改为获取有效单元格内容
//                                double left = lineArray.get(0).getAsJsonObject().get("left").getAsDouble();
                                double left = findValidTableCell(lineArray).get("left").getAsDouble();//
                                if (pageModel.getWidth() / 5 > left && left > 0) {
                                    if (isEmpty(combineLine)) {
                                        continue;
                                    }
                                    String key = combineLine.split(findSY_Format_Split_Char(combineLine), 2)[0];
//                                    extractItem = syTokenizer.splitWords(extractBizModel, combineLine);
                                    try {
                                        extractItem = syTokenizer.splitWords(extractBizModel, key);
                                        if (extractItem != null) {
                                            if (isSY_Format(combineLine)) {
                                                setKV(lineArray, extractItem);
                                                setExtractItemPageNumber(pageModel, extractItem);
                                                logger.info("抽取{}章名词释义业务模型，分析到名词释义业务模型数据，指标名称：{}，分析文本，{}。", getChapterModel().getChapterId(), extractItem.getItemName(), combineLine);
                                            } else {
                                                KVText kText = new KVText();
                                                kText.setPageNumber(pageModel.getPageNumber());
                                                kText.setText(combineLine);
                                                kText.add(createKVTextLayout(pageModel, lineArray));
                                                extractItem.setKey(kText);
                                            }
                                        }
                                    } catch (Exception e) {
                                        logger.error(e.getMessage(), e);
                                    }
                                }
                            }
                        } else {
                            logger.info("抽取{}章名词释义业务模型,数据格式为表格形式，表格数据：{}。", getChapterModel().getChapterId(), extractTableDataList);
                            ExtractItem targetExtractItem = null;
                            if (extractTableDataList.size() == 1) {
                                logger.info("抽取{}章名词释义业务模型,该页码含误导信息，实际数据格式为文本形式，适用默认文本值进行分析，分析文本：{}。", getChapterModel().getChapterId(), pageModel.getJsonTextData());
                                JsonElement tableData = str2Json(extractTableDataList.get(0));
                                JsonObject jsonObject = tableData.getAsJsonArray().get(0).getAsJsonObject();
                                JsonArray dataArray = jsonObject.get("data").getAsJsonArray();
                                if (dataArray.size() <= 3) {
                                    logger.info("抽取{}章名词释义业务模型,执行到该处应该为名词释义首页，该页码含误导信息，实际数据格式为文本形式，适用默认文本值进行分析，分析文本：{}。", getChapterModel().getChapterId(), pageModel.getJsonTextData());
                                    //不解析表格，此处表格数据可能是章节标题为表格形式，跳过该表格，直接进行文本提取
                                    JsonElement pageTextData = pageModel.getJsonTextData();
                                    jsonObject = pageTextData.getAsJsonArray().get(0).getAsJsonObject();
                                    dataArray = jsonObject.get("data").getAsJsonArray();
                                    ExtractItem extractItem = null;
                                    for (int j = 0; j < dataArray.size(); j++) {
                                        JsonArray lineArray = dataArray.get(j).getAsJsonArray();
                                        double left = lineArray.get(0).getAsJsonObject().get("left").getAsDouble();
                                        if (pageModel.getWidth() / 5 > left && left > 0) {
                                            String combineLine = combineTextJsonArray(lineArray);
                                            if (isEmpty(combineLine)) {
                                                continue;
                                            }
                                            if (isStatement(combineLine)) {
                                                continue;
                                            }
                                            String key = combineLine.split(findSY_Format_Split_Char(combineLine), 2)[0];
                                            try {
                                                extractItem = syTokenizer.splitWords(extractBizModel, key);
                                                if (extractItem != null) {
                                                    if (isSY_Format(combineLine)) {
                                                        setKV(lineArray, extractItem);
                                                        setExtractItemPageNumber(pageModel, extractItem);
                                                        logger.info("抽取{}章名词释义业务模型，分析到名词释义业务模型数据，指标名称：{}，分析文本，{}。", getChapterModel().getChapterId(), extractItem.getItemName(), combineLine);
                                                    } else {
                                                        KVText kText = new KVText();
                                                        kText.setPageNumber(pageModel.getPageNumber());
                                                        kText.setText(combineLine);
                                                        kText.add(createKVTextLayout(pageModel, lineArray));
                                                        extractItem.setKey(kText);
                                                    }
                                                }
                                            } catch (Exception e) {
                                                logger.error(e.getMessage(), e);
                                            }
                                        }
                                    }
                                } else {
                                    for (int k = 0; k < dataArray.size(); k++) {
                                        JsonArray itemArray = dataArray.get(k).getAsJsonArray();
                                        if (itemArray.size() <= 1) {
                                            continue;
                                        }
//                                        String key = itemArray.get(0).getAsJsonObject().get("text").getAsString();
                                        try {
                                            List<JsonObject> validJsonObjectList = findValidTableCells(itemArray);
                                            if (validJsonObjectList.isEmpty()) {
                                                continue;
                                            }
                                            JsonObject keyJsonObject = validJsonObjectList.get(0);
                                            if (keyJsonObject == null) {
                                                continue;
                                            }
                                            String key = keyJsonObject.get("text").getAsString();//itemArray.get(0).getAsJsonObject().get("text").getAsString();
                                            int offset = 0;
                                            if (isEmpty(key)) {
                                                offset = 1;
                                                key = validJsonObjectList.get(offset).getAsJsonObject().get("text").getAsString();
                                            }
                                            if (NumberUtils.isDigits(key)) {
                                                offset = 1;
                                                key = validJsonObjectList.get(offset).getAsJsonObject().get("text").getAsString();
                                            }
                                            logger.info("抽取{}章名词释义业务模型,分析表格数据，第{}条，分析文本：{}。", getChapterModel().getChapterId(), (k + 1), itemArray.toString());
                                            targetExtractItem = syTokenizer.splitWords(extractBizModel, key);
                                            if (targetExtractItem != null) {
                                                setSY_TableKV(offset, validJsonObjectList, targetExtractItem, pageModel);
                                                setExtractItemPageNumber(pageModel, targetExtractItem);
                                                logger.info("抽取{}章名词释义业务模型,分析到释义模型业务指标，指标名称：{}，分析文本：{}。", getChapterModel().getChapterId(), targetExtractItem.getItemName(), itemArray.toString());
                                                continue;
                                            }
                                        } catch (Exception e) {
                                            logger.error(e.getMessage(), e);
                                        }
                                    }
                                }
                            } else if (extractTableDataList.size() > 1) {
                                for (int j = 0; j < extractTableDataList.size(); j++) {
                                    JsonElement tableData = str2Json(extractTableDataList.get(j));
                                    JsonObject jsonObject = tableData.getAsJsonArray().get(0).getAsJsonObject();
                                    JsonArray dataArray = jsonObject.get("data").getAsJsonArray();
                                    for (int k = 0; k < dataArray.size(); k++) {
                                        JsonArray itemArray = dataArray.get(k).getAsJsonArray();
                                        if (itemArray.size() <= 1) {
                                            continue;
                                        }
                                        try {
                                            List<JsonObject> validJsonObjectList = findValidTableCells(itemArray);
                                            if (validJsonObjectList.isEmpty()) {
                                                return;
                                            }
                                            JsonObject keyJsonObject = validJsonObjectList.get(0);//findValidTableCell(itemArray);
                                            if (keyJsonObject == null) {
                                                continue;
                                            }
//                                            String key = findValidTableCell(itemArray).getAsJsonObject().get("text").getAsString();//itemArray.get(0).getAsJsonObject().get("text").getAsString();
                                            String key = keyJsonObject.get("text").getAsString();//itemArray.get(0).getAsJsonObject().get("text").getAsString();
                                            int offset = 0;
                                            if (isEmpty(key)) {
                                                offset = 1;
                                                key = validJsonObjectList.get(offset).getAsJsonObject().get("text").getAsString();
                                            }
                                            if (NumberUtils.isDigits(key)) {
                                                offset = 1;
                                                key = validJsonObjectList.get(offset).getAsJsonObject().get("text").getAsString();
                                            }
                                            logger.info("抽取{}章名词释义业务模型,分析表格数据，第{}条，分析文本：{}。", getChapterModel().getChapterId(), (j + 1), itemArray.toString());
                                            targetExtractItem = syTokenizer.splitWords(extractBizModel, key);
                                            if (targetExtractItem != null) {
                                                setSY_TableKV(offset, validJsonObjectList, targetExtractItem, pageModel);
                                                setExtractItemPageNumber(pageModel, targetExtractItem);
                                                logger.info("抽取{}章名词释义业务模型,分析到释义业务指标，指标名称：{}，分析文本：{}。", getChapterModel().getChapterId(), targetExtractItem.getItemName(), itemArray.toString());
                                                continue;
                                            }
                                        } catch (Exception e) {
                                            logger.error(e.getMessage(), e);
                                        }
                                    }
                                }
                            }
                        }
                        break;
                    case TABLE:
                        break;
                    case DEFAULT:
                        break;
                }
            } else {
                logger.error("抽取{}章节名词释义业务模型,当前页码：{}，PDF文档：{}，不存在，请检查！", getChapterModel().getChapterId(), pageModel.getPageNumber(), getDocumentModel().getPdfFilePath());
            }
        }
        syTokenizer.unloadCoreSynonyms(extractBizModel);
        long t2 = System.currentTimeMillis();
        logger.info("抽取{}章节名词释义业务模型数据完成，耗时：{}毫秒。", getChapterModel().getChapterId(), (t2 - t1));
    }
}
